package cn.ffcs.uec.staff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffInfomationVo;
import cn.ffcs.uec.staff.dao.UecAssDao;
import cn.ffcs.uec.staff.model.UecAss;
import cn.ffcs.uec.staffAndOutStaff.vo.StaffAndOutStaff;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecAssDao")
@Transactional
public class UecAssDaoImpl extends BaseDaoImpl implements UecAssDao {

	@Override
	public PageInfo queryPageInfoByUecAss(UecAss uecAss, int current,
			int pageSize) {
		if (null != uecAss) {
			StringBuilder sb = new StringBuilder(
					" SELECT * FROM UEC_ASS T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
			// 判断这个对象是外包人员的数据还是合同制的数据
            if (BaseUnitConstants.NM_OUT_STAFF_TYPE.equals(uecAss
                    .getNmStaffType())) {
                sb.append(" AND T.NM_STAFF_TYPE = ? ");
                params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
            }
			if (!StrUtil.isNullOrEmpty(uecAss.getStaffId())) {
                sb.append(" AND T.STAFF_ID = ? ");
                params.add(uecAss.getStaffId());
            }
			if (!StrUtil.isNullOrEmpty(uecAss.getPkPsndoc())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSNDOC = ? ");
				params.add(uecAss.getPkPsndoc());
			}
			
			return this.jdbcFindPageInfo(sb.toString(), params, current,
					pageSize, UecAss.class);
		}
		return null;
	}

	@Override
	public UecAss findUecAssById(UecAss uecAss) {
		if (uecAss != null) {
			StringBuilder sb = new StringBuilder(
					"select * from UEC_ASS T where 1=1 ");
			List<Object> params = new ArrayList<Object>();

			if (!StrUtil.isNullOrEmpty(uecAss.getAssId())) {
				sb.append(" AND T.ASS_ID = ? ");
				params.add(uecAss.getAssId());
			}

			List<UecAss> uecAssList = this.jdbcFindList(sb.toString(), params,
					UecAss.class);

			if (uecAssList.size() > 0) {
				return uecAssList.get(0);
			}
		}

		return null;
	}

	@Override
	public List<UecAss> findUecAssByStaffId(UecOutStaffInfo uecoutStaffInfo) {
		StringBuilder sb = new StringBuilder(
				" SELECT * FROM UEC_ASS T WHERE 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		sb.append(" AND T.STATUS_CD = ? ");
		params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
         sb.append(" AND T.NM_STAFF_TYPE = ? ");
            params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
            sb.append(" AND T.GLBDEF1 = '1001A11000000000061S'");  
		if (!StrUtil.isNullOrEmpty(uecoutStaffInfo.getOutStaffId())) {
            sb.append(" AND T.STAFF_ID = ? ");
            params.add(uecoutStaffInfo.getOutStaffId());
        }
		return this.jdbcFindList(sb.toString(), params,UecAss.class);
	}

	@Override
	public List<UecAss> findUecAssByOutAndStaffId(UecOutStaffInfomationVo uecOutStaffInfomationVo) {
		StringBuilder sb = new StringBuilder(
				" SELECT * FROM UEC_ASS T WHERE 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		sb.append(" AND T.STATUS_CD = ? ");
		params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
		if (!StrUtil.isNullOrEmpty(uecOutStaffInfomationVo.getOutStaffId()) && uecOutStaffInfomationVo.getOutStaffId()>0) {
            sb.append(" AND T.STAFF_ID = ? ");
            params.add(uecOutStaffInfomationVo.getOutStaffId());
            sb.append(" AND T.NM_STAFF_TYPE = ? ");
            params.add(BaseUnitConstants.NM_OUT_STAFF_TYPE);
            sb.append(" AND T.GLBDEF1 = '1001A11000000000061S'");
        }
		/*if(!StrUtil.isNullOrEmpty(uecOutStaffInfomationVo.getStaffId())) {
			    sb.append(" AND T.PK_PSNDOC = ? ");
	            params.add(uecOutStaffInfomationVo.getStaffId());
	            sb.append(" AND T.NM_STAFF_TYPE IS NULL ");
	            sb.append(" AND T.GLBDEF1 = '1001A11000000000061S'");
		}*/
		return this.jdbcFindList(sb.toString(), params,UecAss.class);
	}
	@Override
	public List<UecAss> findUecAssByPkPsndoc(StaffAndOutStaff staffAndOutStaff) {
		StringBuilder sb = new StringBuilder(
				" SELECT * FROM UEC_ASS T WHERE 1 = 1 ");
		List<Object> params = new ArrayList<Object>();
		sb.append(" AND T.STATUS_CD = ? ");
		params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
            sb.append(" AND T.GLBDEF1 = '1001A11000000000061S'");  
		if (!StrUtil.isNullOrEmpty(staffAndOutStaff.getPkPsndoc())) {
            sb.append(" AND T.PK_PSNDOC = ? ");
            params.add(staffAndOutStaff.getPkPsndoc());
        }
		sb.append(" ORDER BY T.BEGINDATE ");
		return this.jdbcFindList(sb.toString(), params,UecAss.class);
	}
}
